package com.adulcimer.services;

import cn.dev33.satoken.stp.StpUtil;
import com.adulcimer.models.entity.domain.User;
import com.adulcimer.models.entity.service.UserService;
import com.adulcimer.models.enums.AccountEnum;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;

/**
 * @author Simon
 * @create 2025/2/6
 * @Description
 **/

public  abstract class BaseService {

    @Resource
    protected HttpServletRequest request;

    @Resource
    protected UserService userService;


    /**
     * 获取请求中的token
     */

    protected String getToken() {
        return StpUtil.getTokenValue();
    }


    /**
     * 获取请求中的userId
     */
    protected Long getUserId() {
        return StpUtil.getLoginIdAsLong();
    }

    /**
     * 获取请求中的userAgent
     */
    protected String getUserAgent() {
        return request.getHeader("User-Agent");
    }

    /**
     * 获取请求中的ip
     */
    protected String getIp() {return request.getRemoteAddr();}

    /**
     * 获取当前用户是否是admin
     */
    protected boolean isAdmin() {
        return userService.getById(getUserId()).getSysRole().equals(AccountEnum.ADMIN.getValue());
    }

    /**
     * 获取系统管理员id
     */
    protected Long getAdminId() {
        return userService.lambdaQuery().eq(User::getSysRole, AccountEnum.ADMIN.getValue()).one().getId();
    }
}